<script setup lang="ts">
 import { ElMessageBox,ElMessage } from 'element-plus'
 import {reqAllCategory,reqAddCategory,reqUpdateCategory,reqDeleteCategory} from '@/api/catApi'
 import { ref,onMounted } from 'vue'
 import {
    Edit,
    Delete,Plus
} from '@element-plus/icons-vue'


let breed = ref({
  id: '0',
  code: '001',
  caption: '波斯猫'
})
//分类数据
let breeds = ref([
{
    id: '0',
    code: '001',
    caption: '波斯猫',
  },
])

onMounted(() => {
  fetchBreedData()
})
//获取分类数据
const fetchBreedData = async() => {
  const res = await reqAllCategory()
  console.log(res.data.data)
  breeds.value = res.data.data
}
//添加分类数据
const doAdd = async() => {
  const res = await reqAddCategory(breed.value)
  console.log(res.data.data)
  //刷新表格
  fetchBreedData()
}
//编辑分类数据
const doSave = async() => {
  const res = await reqUpdateCategory(breed.value)
  console.log(res.data.data)
  //编辑后更新数据
  fetchBreedData()
}

//每一行的删除
const doDelete = (row: any) => {
  console.log(row)
  breed.value = row
  ElMessageBox.confirm('确定要删除', '警告', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
    center: true, // 让弹框垂直居中
  })
    .then(async() => {
      const res = await reqDeleteCategory(breed.value.id)
      console.log(res.data.data)
      //删除成功后更新数据
      fetchBreedData()
      ElMessage({
        type: 'success',
        message: '删除成功',
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}


//每一行的编辑
const doEdit = async (row:any) => {
  console.log(row)
  breed.value = row
  const res = await reqUpdateCategory(breed.value)
  console.log(res)
}

</script>

<template>
<h5>猫咪品种管理</h5>
<!-- 顶部表单 -->
  <!-- 表单 -->
  <el-form :model="breed" label-position="right">
    <el-row :gutter="20">
      <el-col :span="5">
        <el-form-item label="ID:">
          <el-input v-model="breed.id" readonly />
        </el-form-item>
      </el-col>
      <el-col :span="3">
        <el-form-item label="编号:">
          <el-input v-model="breed.code" minlength="1" maxlength="3" show-word-limit />
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="名称:">
          <el-input v-model="breed.caption" minlength="1" maxlength="10" show-word-limit />
        </el-form-item>
      </el-col>
      <el-form-item>
        <el-button type="success" @click="doAdd()">
          <el-icon>
            <Plus />
          </el-icon>
          新增
        </el-button>
        <el-button type="primary" @click="doSave()">
          <el-icon>
            <Edit />
          </el-icon>
          保存
        </el-button>
      </el-form-item>
    </el-row>
  </el-form>
<!-- 表格 -->
  <el-table :data="breeds" style="width: 100%">
    <el-table-column prop="id" label="ID" width="180" header-align="center" align="center"/>
    <el-table-column prop="code" label="编号" width="180" header-align="center" align="center"/>
    <el-table-column prop="caption" label="标题" width="180" header-align="center" align="center"/>
     <el-table-column label="操作" header-align="center" align="center">
                <template #default="scope">
                    <el-button :icon="Edit" plain type="primary" @click="doEdit(scope.row)">修改</el-button>
                    <el-button :icon="Delete" plain type="danger" @click="doDelete(scope.row)">删除</el-button>                     
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
  </el-table>

</template>

<style scoped>

</style>
